package jdbcmapper;

import java.sql.Connection;
import java.util.Hashtable;

import lotus.domino.*;
import java.util.Vector;

/**
 * <p>Title: JDBC Mapper</p>
 *
 * <p>Description: Libraries to copy documents form Lotus Notes to jdbc
 * table</p>
 *
 * <p>Copyright: Copyright (c) 2005</p>
 *
 * <p>Company: The Salvation ARmy</p>
 *
 * @author Ansel Robateau
 * @version 1.1
 */
public class SQLDB extends NotesDoc{
    public static final String SQLDATABASE = "SQLDatabase";
    public static final String DRIVER = "Driver";
    public static final String URL = "URL";
    public static final String USERNAME = "Username";
    public static final String PASSWORD = "Password";
    private Hashtable sqltables;
    private NotesForm m_parent;
    public SQLDB(Session session,Document doc) {
        super(session,doc);
        this.sqltables = new Hashtable();
    }
    public Table getFirstTable(){
        return (Table)this.getFirst();
    }
    public Table getNextTable(){
        return (Table)this.getNext();
    }
    public NotesForm getParent(){
        return this.m_parent;
    }
    /**
     * LoadMapping
     *
     * @param map FieldMap
     */
    public void LoadMapping(NotesForm form,FieldMap map) throws Exception{
        this.m_parent = form;
        String sqltable = map.getString(FieldMap.SQLTABLE);
        try{
            if(sqltables.get(sqltable) == null){
                throw new Exception();
            }
        }catch(Exception e){
            //View view = this.getDocument().getParentDatabase().getView("SQL Tables");
            Vector keys = new Vector();
            keys.add(map.getString(FieldMap.SQLDATABASE));
            keys.add(map.getString(FieldMap.SQLTABLE));
            //ViewEntryCollection vc = view.getAllEntriesByKey(keys);
            //ViewEntry entry = vc.getFirstEntry();
            String str = "Form = \"SQL Table\" & "+Table.SQLTABLE+" = \"";
            str += map.getString(map.SQLTABLE)+"\" & "+Table.SQLDATABASE;
            str += " = \"" + map.getString(map.SQLDATABASE) + "\"";
            //System.out.println(str);
            this.getM_log().add(str);
            DocumentCollection col = this.getDocument().getParentDatabase().search(str);
            //System.out.println("Documents found = " + col.getCount());
            this.getM_log().add("Documents found = " + col.getCount());
            Document doc = col.getFirstDocument();

            if(doc != null){
                //doc = entry.getDocument();
                Table table = new Table(this.getSession(),doc);
                this.m_subs.add(table);
                table.LoadMapping(this,map);
                this.sqltables.put(sqltable,"");
            }
        }
    }

    /**
     * getConnection
     *
     * @return Connection
     */
    public Connection getConnection() throws Exception{
        Connection con = this.getParent().getParent().getParent().getParent().getConnection(
            this.getString(SQLDB.SQLDATABASE));
        return con;
    }

    public Vector getM_log() {
        return this.getParent().getM_log();
    }
}
